package com.chujian.framework.core;

import com.chujian.framework.util.Spring;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import java.io.Serializable;

/**
 * ID生成器
 */
@Slf4j
public class IdGenerator implements InitializingBean, ApplicationContextAware {

    // 赋值..
    private ApplicationContext applicationContext;
    public static IIdGen idGen;

    public static Serializable genId() {
        if (null == idGen) {
            log.error("idGen is null ...");
            return null;
        }
        return idGen.genId();
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        idGen = Spring.getBean(this.applicationContext, IIdGen.class);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
